Programming Questions for Practice
You are given with an array of integers, arr, of size array_length. Find the index, i, of the first integer num in array arr, that can be represented as a product of two integers A and B. A and B have indices j and k, respectively. If there is no such integer, return -1.
#include <stdio.h>
#include <stdlib.h>
/*
You are given with an array of integers, arr, of size array_length.
Find the index, i, of the first integer num in array arr, that can
be represented as a product of two integers A and B. A and B have
indices j and k, respectively.
If there is no such integer, return -1.
*/
int findNum(int array_length, int *arr) {
int i;
if(array_length == 1)
return -1;
else {
for(i = 0; i < array_length; i++) {
if(arr[i]*arr[i] == arr[i+1]) {
return i+1;
}
}
return -1;
}
}
int main() {
int arr[] = {5, 25};
int length = sizeof(arr)/sizeof(arr[0]);
int result = findNum(length, arr);
printf("%d", result);
return 0;
}